package com.base.service;

import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import tool.ClientTool;
import tool.JsonTool;
import tool.TimeTool;

import com.base.bean.*;
import com.base.dao.*;
import com.base.domain.*;

/**
 * 
 * @author Administrator
 * 
 */

public class UserService {

	// 单例模式 start
	private volatile static UserService instance = null;
	private UserDao userDao;


	private UserService() {
		userDao = (UserDao) _BaseDao.ac.getBean("UserDao");
	
	}

	public static UserService getInstance() {
		if (instance == null) {
			synchronized (UserService.class) {// 1
				if (instance == null) {// 2
					instance = new UserService();// 3
				}
			}
		}
		return instance;
	}

	// 单例模式 end

	/**
	 * @return uid=-1 表示出现sql Exception
	 */
	public User getByOpenId(String openid){
	try{	
		return userDao.findByHql("from User u where u.openid='"+openid+"'").get(0);
	}catch(Exception e){
		//e.printStackTrace();
		return new User(-1);
	}
	}
	
	public User getUserById(Integer uid){
		try{
		return userDao.findByHql("from User u where u.id='"+uid+"'").get(0);
	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	

	// TEST
	public static void main(String[] args) throws Exception {
		UserService us=UserService.getInstance();
		AddressService as=AddressService.getInstance();
		
		User u=new User();
		u.setName("这种");
		u.setCompany("快快快");
		u.setCity(as.getNameByCode(AddressService.ADD_TYPE.CITY, "130200"));
		us.userDao.merge(u);

	}

	public UserDao getUserDao() {
		return userDao;
	}

}
